//
//  FZNewfeatureViewController.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/23/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

private let NewfeatureViewID = "ItemID"

class FZNewfeatureViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        setupLayout()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
//MARK:==========================加载控件与布局==========================
    
    func setupUI() {
        
        view.addSubview(newFeatureView)
        
        
    }
    func setupLayout() {
        newFeatureView.snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
    }

//MARK:==========================懒加载==========================
    
    lazy var newFeatureView: UICollectionView = {
       
        let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
        
        flowLayout.itemSize = CGSizeMake(SCREENW, SCREENH)
        flowLayout.scrollDirection = .Horizontal
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
        let view: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.pagingEnabled = true
        view.bounces = false
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.dataSource = self
        view.delegate = self
        
        view.registerClass(FZNewfeatureCell.self, forCellWithReuseIdentifier: NewfeatureViewID)
        return view
    }()
}

extension FZNewfeatureViewController: UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let item = collectionView.dequeueReusableCellWithReuseIdentifier(NewfeatureViewID, forIndexPath: indexPath) as! FZNewfeatureCell
        
        item.contentView.backgroundColor = RANDRGB()
        item.index = indexPath.item
        return item
    }
    
    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return false
    }
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let index = NSInteger(scrollView.contentOffset.x / CGFloat(SCREENW))
        if index != 3 {
            return
        }
        let cell = newFeatureView.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! FZNewfeatureCell
        
        cell.featureButton.hidden = false
        cell.featureButton.userInteractionEnabled = false
        
        cell.featureButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView .animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { 
            
                cell.featureButton.transform = CGAffineTransformIdentity
            
            }) { (_) in
                cell.featureButton.userInteractionEnabled = true
        }
    
        
    }
}
